<template>
  <div id="app">
    <router-view v-if="isRouterAlive" />
  </div>
</template>

<script>

export default {
  name: 'App',
  provide() {
    return {
      reload: this.reload,
      goUrl: this.goUrl,
      backUrl: this.backUrl,
      formatDate: this.formatDate,
      delOne: this.delOne,
      delMany: this.delMany,
      renew: this.renew,
      archives: this.archives,
      addOne: this.addOne,
      getOne: this.getOne,
      update: this.update,
      appendOtherAdmin: this.appendOtherAdmin,
      delOtherAdmin: this.delOtherAdmin,
      getStatusFormat: this.getStatusFormat,
      getCommonStatusFormat: this.getCommonStatusFormat,
      getWeightFormat: this.getWeightFormat,
      getTypeFormat: this.getTypeFormat,
      getPreferenceFormat: this.getPreferenceFormat,
      getEnterFormat: this.getEnterFormat,
      appendParts: this.appendParts,
      removeParts: this.removeParts,
      getRankFormat: this.getRankFormat,
      getModeFormat: this.getModeFormat,
      getApplyFormat: this.getApplyFormat,
      getApplyKindFormat: this.getApplyKindFormat,
      getApplyPartsFormat: this.getApplyPartsFormat,
      getOrderPartsFormat: this.getOrderPartsFormat,
      getEnterPartsFormat: this.getEnterPartsFormat,
      getWordFormat: this.getWordFormat,
      getAppearFormat: this.getAppearFormat,
      getAppearKindFormat: this.getAppearKindFormat,
      getHandBackFormat: this.getHandBackFormat,
      getParamCategoryFormat: this.getParamCategoryFormat,
      getTurnFormat: this.getTurnFormat,
      getEqStatusFormat: this.getEqStatusFormat,
      getGroupTypeFormat: this.getGroupTypeFormat,
      getCmFormat: this.getCmFormat,
      getToolAppearStatusFormat: this.getToolAppearStatusFormat,
      getApproveStatusFormat: this.getApproveStatusFormat,
      getPartsTakeFormat: this.getPartsTakeFormat,
      getPlanCategoryFormat: this.getPlanCategoryFormat,
      getPlanWorkshopFormat: this.getPlanWorkshopFormat,
      getPlanProfessionFormat: this.getPlanProfessionFormat,
      getPlanTypeFormat: this.getPlanTypeFormat,
      getPlanPriorityFormat: this.getPlanPriorityFormat,
      getPlanPeriodFormat: this.getPlanPeriodFormat,
      getPlanStatusFormat: this.getPlanStatusFormat,
      getScope: this.getScope,
      isEdit: this.isEdit,
      selectName: this.selectName,
      selectValue: this.selectValue,
      isNeedEngineer: this.isNeedEngineer,
      changePmStatus: this.changePmStatus
    }
  },
  data() {
    return {
      isRouterAlive: true,
      thisData: {}
    }
  },
  methods: {
    reload() {
      this.isRouterAlive = false
      this.$nextTick(() => { this.isRouterAlive = true })
    },
    goUrl(url) {
      this.$router.push({
        path: url
      })
    },
    backUrl() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    },
    formatDate(d) {
      const date = new Date(d)
      const YY = date.getFullYear() + '-'
      const MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      const DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
      const hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
      const mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
      const ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
      return YY + MM + DD + ' ' + hh + mm + ss
    },
    delOne(API, id, goBack) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.delete(id).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            if (goBack) {
              this.backUrl()
            } else {
              this.reload()
            }
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    archives(API, selection) {
      const ids = selection
        .map((item) => {
          return item.id
        })
        .join(',')
      API.archives({ id: ids }).then((response) => {
        console.log(response)
        if (response.data.error_code === 1) {
          this.$message({
            message: '恭喜你，归档成功',
            type: 'success'
          })
          this.reload()
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    delMany(API, selection) {
      const ids = selection
        .map((item) => {
          return item.id
        })
        .join(',')
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.delete(ids).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            this.reload()
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    renew(API, selection) {
      const ids = selection
        .map((item) => {
          return item.id
        })
        .join(',')
      this.$confirm('此操作将恢复该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.renew(ids).then(response => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，恢复成功',
              type: 'success'
            })
            this.reload()
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    addOne(API, thisData, thisForm) {
      thisForm.validate((valid) => {
        if (valid) {
          API.create(thisData).then((response) => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，新增成功',
                type: 'success'
              })
              this.backUrl()
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    getOne(API, id, errorCode, errorMessage) {
      API.getOne(id).then(response => {
        if (response.data.error_code === errorCode) {
          this.$alert(errorMessage, '错误', {
            confirmButtonText: '确定',
            callback: () => {
              this.backUrl()
            }
          })
        }
        this.thisData = response.data
      })
    },
    update(API, thisData, thisForm) {
      thisForm.validate((valid) => {
        if (valid) {
          API.update(thisData).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              this.backUrl()
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    appendOtherAdmin(API, thisForm, admin_data, errorCode, errorMessage) {
      thisForm.validate((valid) => {
        if (valid) {
          API.otherAdmin(admin_data).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              // API.getOne(this.$route.params.id).then(response => {
              //   this.list = response.data.other_admin
              //   this.reload()
              // })
              this.reload()
            } else if (response.data.error_code === errorCode) {
              this.$message.error(errorMessage)
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    delOtherAdmin(API, id, admin_data) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.delOtherAdmin(admin_data).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            // API.getOne(this.$route.params.id).then(response => {
            //   this.list = response.data.other_admin
            // })
            this.reload()
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    appendParts(API, thisForm, parts_data, errorCode, errorMessage) {
      thisForm.validate((valid) => {
        if (valid) {
          API.append(parts_data).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              // API.getOne(this.$route.params.id).then(response => {
              //   this.list = response.parts_list
              // })
              this.reload()
            } else if (response.data.error_code === errorCode) {
              this.$message.error(errorMessage)
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    removeParts(API, id, parts_data) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.remove(parts_data).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            // API.getOne(this.$route.params.id).then(response => {
            //   this.list = response.parts_list
            // })
            this.reload()
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    getEqStatusFormat(row) {
      if (row.status === 0) {
        return this.$createElement('div', { 'style': { 'color': 'green' }}, '在用')
      } else if (row.status === 1) {
        return this.$createElement('div', { 'style': { 'color': 'red' }}, '停用')
      } else if (row.status === 2) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '拆除')
      } else if (row.status === 3) {
        return this.$createElement('div', { 'style': { 'color': 'blue' }}, '待报废')
      } else if (row.status === 4) {
        return this.$createElement('div', { 'style': { 'color': 'orange' }}, '维修中')
      } else if (row.status === 5) {
        return this.$createElement('div', { 'style': { 'color': 'purple' }}, '委外')
      }
    },
    getStatusFormat(row) {
      if (row.status === 0) {
        return this.$createElement('div', { 'style': { 'color': 'blue' }}, '测试')
      } else if (row.status === 1) {
        return this.$createElement('div', { 'style': { 'color': 'green' }}, '启用')
      } else if (row.status === 2) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '临时移除')
      } else if (row.status === 3) {
        return this.$createElement('div', { 'style': { 'color': 'red' }}, '停用')
      }
    },
    getCommonStatusFormat(row) {
      if (row.status === 0) {
        return this.$createElement('div', { 'style': { 'color': 'green' }}, '启用')
      } else if (row.status === 1) {
        return this.$createElement('div', { 'style': { 'color': 'red' }}, '停用')
      }
    },
    getWeightFormat(row) {
      if (row.weight === 0) {
        return '未定义'
      } else if (row.weight === 1) {
        return '至关重要'
      } else if (row.weight === 2) {
        return '重要'
      } else if (row.weight === 3) {
        return '一般'
      }
    },
    getTypeFormat(row) {
      if (row.type === 1) {
        return '自定义'
      } else if (row.type === 0) {
        return '系统'
      }
    },
    getPreferenceFormat(row) {
      if (row.preference === 0) {
        return '最优先'
      } else if (row.preference === 1) {
        return '第二优先'
      } else if (row.preference === 2) {
        return '第三优先'
      }
    },
    getEnterFormat(row) {
      if (row.type === 0) {
        return '采购'
      } else if (row.type === 1) {
        return '赠予'
      } else if (row.type === 2) {
        return '自制'
      } else if (row.type === 3) {
        return '修旧利废'
      }
    },
    getRankFormat(row) {
      if (row.rank === 0) {
        return '一级'
      } else if (row.rank === 1) {
        return '二级'
      } else if (row.rank === 2) {
        return '三级'
      }
    },
    getModeFormat(row) {
      if (row.mode === 0) {
        return '电气'
      } else if (row.mode === 1) {
        return '机械'
      } else if (row.mode === 2) {
        return '通讯'
      } else if (row.mode === 3) {
        return '其他'
      }
    },
    getApplyFormat(row) {
      if (row.status === 0) {
        return this.$createElement('div', { 'style': { 'color': 'blue' }}, '分配工单')
      } else if (row.status === 1) {
        return this.$createElement('div', { 'style': { 'color': 'orange' }}, '工单在执行')
      } else if (row.status === 2) {
        return this.$createElement('div', { 'style': { 'color': 'green' }}, '工单执行完毕')
      }
    },
    getApplyKindFormat(row) {
      if (row.kind === 1) {
        return '备件'
      } else if (row.kind === 2) {
        return '工具'
      }
    },
    getApplyPartsFormat(row) {
      if (row.status === 0) {
        return this.$createElement('div', { 'style': { 'color': 'blue' }}, '创建')
      } else if (row.status === 1) {
        return this.$createElement('div', { 'style': { 'color': 'red' }}, '待审核（工程师）')
      } else if (row.status === 2) {
        return this.$createElement('div', { 'style': { 'color': 'red' }}, '待审核（科长）')
      } else if (row.status === 3) {
        return this.$createElement('div', { 'style': { 'color': 'green' }}, '申请完成')
      } else if (row.status === 4) {
        return this.$createElement('div', { 'style': { 'color': 'red' }}, '待审核（工程师）')
      } else if (row.status === 5) {
        return this.$createElement('div', { 'style': { 'color': 'red' }}, '待审核（科长）')
      } else if (row.status === 6) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '入库完成')
      } else if (row.status === 7) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '归档')
      }
    },
    getOrderPartsFormat(row) {
      if (row.status === 0) {
        return this.$createElement('div', { 'style': { 'color': 'blue' }}, '创建')
      } else if (row.status === 1) {
        return this.$createElement('div', { 'style': { 'color': 'green' }}, '审核完成')
      } else if (row.status === 2) {
        return this.$createElement('div', { 'style': { 'color': 'red' }}, '退回')
      } else if (row.status === 3) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '入库完成')
      } else if (row.status === 4) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '归档')
      }
    },
    getEnterPartsFormat(row) {
      if (row.status === 0) {
        return this.$createElement('div', { 'style': { 'color': 'blue' }}, '创建')
      } else if (row.status === 1) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '入库完成')
      } else if (row.status === 2) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '归档')
      }
    },
    getWordFormat(row) {
      if (row.status === 0) {
        return '创建'
      } else if (row.status === 1) {
        return '审核完成'
      } else if (row.status === 2) {
        return '在执行'
      } else if (row.status === 3) {
        return '验证完成'
      } else if (row.status === 4) {
        return '归档'
      }
    },
    getAppearFormat(row) {
      if (row.status === 0) {
        return this.$createElement('div', { 'style': { 'color': 'blue' }}, '创建')
      } else if (row.status === 1) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '出库完成')
      } else if (row.status === 2) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '归档')
      }
    },
    getAppearKindFormat(row) {
      if (row.status === 0) {
        return '领用'
      } else if (row.status === 1) {
        return '借用'
      }
    },
    getHandBackFormat(row) {
      if (row.status === 0) {
        return this.$createElement('div', { 'style': { 'color': 'blue' }}, '创建')
      } else if (row.status === 1) {
        return this.$createElement('div', { 'style': { 'color': 'green' }}, '审核完成')
      } else if (row.status === 2) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '归档')
      }
    },
    getParamCategoryFormat(row) {
      if (row.category === 0) {
        return '测量'
      } else if (row.category === 1) {
        return '增量型的'
      } else if (row.category === 2) {
        return '非增量型'
      } else if (row.category === 3) {
        return '感官'
      }
    },
    getTurnFormat(row) {
      if (row.status === 0) {
        return '创建'
      } else if (row.status === 1) {
        return '待审核'
      } else if (row.status === 2) {
        return '财务'
      } else if (row.status === 3) {
        return '主管领导'
      } else if (row.status === 4) {
        return '总经理'
      } else if (row.status === 5) {
        return '生效'
      } else if (row.status === 6) {
        return '完成'
      }
    },
    getGroupTypeFormat(row) {
      if (row.type === 0 || row.gz === 0) {
        return '设备'
      } else if (row.type === 1 || row.gz === 1) {
        return '工装'
      }
    },
    getCmFormat(row) {
      if (row.status === 0) {
        return this.$createElement('div', { 'style': { 'color': 'blue' }}, '待执行')
      } else if (row.status === 1) {
        return this.$createElement('div', { 'style': { 'color': 'orange' }}, '在执行')
      } else if (row.status === 2) {
        return this.$createElement('div', { 'style': { 'color': 'orange' }}, '待班长确认')
      } else if (row.status === 3) {
        return this.$createElement('div', { 'style': { 'color': 'orange' }}, '待工程师确认')
      } else if (row.status === 4) {
        return this.$createElement('div', { 'style': { 'color': 'orange' }}, '待线上确认')
      } else if (row.status === 5) {
        return this.$createElement('div', { 'style': { 'color': 'green' }}, '待总结')
      } else if (row.status === 6) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '总结完成')
      } else if (row.status === 99) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '归档')
      }
    },
    getToolAppearStatusFormat(row) {
      if (row.status === 0) {
        return this.$createElement('div', { 'style': { 'color': 'blue' }}, '创建')
      } else if (row.status === 1) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '出库完成')
      } else if (row.status === 2) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '归档')
      } else if (row.status === 3) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '已归还')
      }
    },
    getApproveStatusFormat(row) {
      if (row.approve === 0) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '待审')
      } else if (row.approve === 1) {
        return this.$createElement('div', { 'style': { 'color': 'green' }}, '通过')
      }
    },
    getPartsTakeFormat(row) {
      if (row.status === 0) {
        return this.$createElement('div', { 'style': { 'color': 'blue' }}, '创建')
      } else if (row.status === 1) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '盘点完成')
      } else if (row.status === 2) {
        return this.$createElement('div', { 'style': { 'color': 'gray' }}, '归档')
      }
    },
    getPlanCategoryFormat(row) {
      if (row.plan_category === 0) {
        return '一般点检'
      } else if (row.plan_category === 1) {
        return '自主维护'
      } else if (row.plan_category === 2) {
        return '专业维护'
      } else if (row.plan_category === 3) {
        return '润滑'
      } else if (row.plan_category === 4) {
        return '专业点检'
      } else {
        return '临时性'
      }
    },
    getPlanWorkshopFormat(row) {
      if (row.workshop === 0) {
        return '冲压车间'
      } else if (row.workshop === 1) {
        return '车身车间'
      } else if (row.workshop === 2) {
        return '涂装车间'
      } else if (row.workshop === 3) {
        return '总装车间'
      } else if (row.workshop === 4) {
        return '生产管理部'
      } else if (row.workshop === 5) {
        return '质量部'
      } else if (row.workshop === 6) {
        return '设备动力部'
      }
    },
    getPlanProfessionFormat(row) {
      if (row.profession === 0) {
        return '所有'
      } else if (row.profession === 1) {
        return '电气'
      } else if (row.profession === 2) {
        return '机械'
      }
    },
    getPlanTypeFormat(row) {
      if (row.type === 0) {
        return '系统'
      } else if (row.type === 1) {
        return '自定义'
      }
    },
    getPlanPriorityFormat(row) {
      if (row.priority === 0) {
        return '所有'
      } else if (row.priority === 1) {
        return '延误48小时以内'
      } else if (row.priority === 2) {
        return '延误超过48小时'
      } else if (row.priority === 3) {
        return '立即'
      }
    },
    getPlanPeriodFormat(row) {
      return row.period + '天'
    },
    getPlanStatusFormat(row) {
      if (row.status === 0) {
        return this.$createElement('div', { 'style': { 'color': 'green' }}, '启用')
      } else if (row.status === 1) {
        return this.$createElement('div', { 'style': { 'color': 'red' }}, '停用')
      } else if (row.status === 2) {
        return this.$createElement('div', { 'style': { 'color': 'red' }}, '请求停产')
      }
    },
    getScope(id) {
      const all = [1, 60, 61, 308]
      const monitorMember = [68, 69, 70, 71, 72, 78, 148, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176,
        177, 178, 179, 180, 197, 198, 199, 200, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218,
        233, 234, 235, 239, 240, 243, 244, 245]
      const bqMonitor = [68, 69, 70, 71]
      // 孙鑫，148是设备动力部的班长，也是设备动力部的推进员
      const tj = [295, 296, 297, 298, 299, 300, 301]
      const engineer = []
      const kz = [47]
      if (all.includes(id)) {
        return 1 // 管理员
      } else if (monitorMember.includes(id)) {
        return 2 // 班长
      } else if (engineer.includes(id)) {
        return 3 // 工程师
      } else if (tj.includes(id)) {
        return 4 // 推进员
      } else if (kz.includes(id)) {
        return 5 // 科长
      } else if (bqMonitor.includes(id)) {
        return 6 // 保全班长
      } else {
        return 99 // 操作者
      }
    },
    isEdit() {
      return !isNaN(this.$route.path.split('/').pop())
    },
    selectName(arr, val) {
      const res = arr.find(item => {
        if (item.value === val) {
          return item
        }
      })
      if (res && res.name) {
        return res.name
      } else {
        return '暂无'
      }
    },
    selectValue(arr, name) {
      const res = arr.find(item => {
        return item.name === name
      })
      console.log(arr)
      if (res && res.value) {
        return res.name
      } else {
        return '无法识别'
      }
    },
    isNeedEngineer(category) {
      const needEngineerCategory = [3, 4]
      return needEngineerCategory.includes(category)
    },
    changePmStatus(API, id, qrcode_time, submit_complate, eq_list) {
      // const statusAgo = this.data.status
      // if (statusAgo !== '超时') {
      // this.$message.error('状态不对，别改了')
      // return false
      // }
      if (!submit_complate) {
        this.$message.error('先填写时间')
        return false
      } else {
        const data = {
          id,
          submit_complate,
          qrcode_time,
          eq_list
        }
        API.jhb(data).then(res => {
          console.log(res)
          if (res.data.error_code === 1) {
            this.$message.success('成功')
            this.reload()
          } else {
            this.$message.error(res.data.message)
          }
        })
      }
    }
  }
}
</script>
